package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.Inventory;
import com.ggk911.pharmacy_system.service.impl.InventoryServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 库存控制层
 *
 * @author ggk-911
 * @version 1.0 2023/4/11 17:13
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/inventory")
public class InventoryController {
    private final InventoryServiceImpl service;

    //GET查询
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               Integer drugInfoId,
                               Integer inventoryNum,
                               Integer drugInfoTypeId,
                               @RequestParam(defaultValue = "") String drugName,
                               @RequestParam(defaultValue = "") String manufacturer,
                               @RequestParam(defaultValue = "") String indications,
                               @RequestParam(defaultValue = "") String nationalCode,
                               @RequestParam(defaultValue = "") String approvalNo) {
        Page<Inventory> inventoryPage = new Page<>(page, size);
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();

        wrapper.eq(drugInfoId != null, "drug_info_id", drugInfoId);
//        wrapper.eq(inventoryNum != null, "inventory_num", inventoryNum);
        wrapper.ge(inventoryNum != null, "inventory_num", inventoryNum);
        wrapper.eq(drugInfoTypeId != null, "drug_info_type.drug_info_type_id", drugInfoTypeId);
        wrapper.and(!"".equals(drugName), a -> a.like("drug_info.drug_info_name", drugName).or().like("drug_info.drug_info_alias", drugName));
        wrapper.like(!"".equals(manufacturer), "drug_info.drug_info_manufacturer", manufacturer);
        wrapper.like(!"".equals(indications), "drug_info.drug_info_indications", indications);
        wrapper.like(!"".equals(approvalNo), "drug_info.drug_info_approval_no", approvalNo);
        wrapper.like(!"".equals(nationalCode), "drug_info.drug_info_national_code", nationalCode);

        service.pageAd(inventoryPage, wrapper);

        return RestResult.ok("查询成功", inventoryPage);
    }

    //GET查询缺货
    @GetMapping("/getLess")
    public RestResult getLess() {
        Integer integer = service.selectInventoryLimit();
        List<Inventory> inventoryList = service.listAd(new QueryWrapper<>()).stream().filter(item -> item.getInventoryNum() < integer).collect(Collectors.toList());
        return RestResult.ok("查询成功", inventoryList);
    }

    //GET获取limit
    @GetMapping("/getLimit")
    public RestResult getLimit() {
        return RestResult.ok("查询成功", service.selectInventoryLimit());
    }

    //PUT修改limit
    @PutMapping("/updateLimit")
    public RestResult updateLimit(Integer limit) {
        if (service.updateInventoryLimit(limit)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //GET类型库存分布
    @GetMapping("/getSumByType")
    public RestResult getSumByType() {
        return RestResult.ok("查询成功", service.selectSumByType());
    }

    //POST添加
    @PostMapping("/insert")
    public RestResult insert(Inventory inventory) {
        if (service.save(inventory)) {
            return RestResult.ok("添加成功");
        }
        return RestResult.error("添加失败");
    }

    //PUT修改
    @PutMapping("/update")
    public RestResult update(Inventory inventory,
                             Integer drugInfoId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper();
        wrapper.eq(drugInfoId != null, "drug_info_id", drugInfoId);

        if (service.update(inventory, wrapper)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer drugInfoId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper();
        wrapper.eq(drugInfoId != null, "drug_info_id", drugInfoId);

        if (service.remove(wrapper)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }
}
